import { _decorator, Component, Label, Node } from 'cc';
import { Core } from '../core/Core';
import { EGameEvent } from '../core/Config';
const { ccclass, property } = _decorator;

@ccclass('StatView')
export class StatView extends Component {

    @property(Node)
    labelAP: Node;

    @property(Node)
    labelDP: Node;

    @property(Node)
    labelHP: Node;

    @property(Node)
    labelEXP: Node;
    

    //初始化
    protected onEnable(): void {
        Core.instance.event.on(EGameEvent.PLAYER_DATA_CHANGED, this.onDataChange, this);
        this.bindPlayerData();
    }

    //销毁
    protected onDisable(): void {
        Core.instance.event.off(EGameEvent.PLAYER_DATA_CHANGED, this.onDataChange, this);
    }

    start() {

    }

    update(deltaTime: number) {
        
    }


    //监听玩家数据变化
    private onDataChange() {
        this.bindPlayerData();
    }

    //绑定玩家数据
    private bindPlayerData() {
        //监听玩家数据变化
        let data=Core.instance.playerData;
        this.labelAP.getComponent(Label).string = `${data.ap}`;
        this.labelDP.getComponent(Label).string = `${data.dp}`;
        this.labelHP.getComponent(Label).string = `${data.hp}`;
        this.labelEXP.getComponent(Label).string = `${data.exp}`;
    }
}


